<?php

namespace app\system\middleware;

use think\Request;

class Auth
{
    /**
     * 处理请求
     *
     * @param Request $request
     * @param \Closure $next
     * @return Response|\think\response\Json
     */
    public function handle(Request $request, \Closure $next)
    {
        //第一步先取token
        $authorization = $request->header('Authorization'); //token
        if(!$authorization){
            return json(['code' => 111, 'msg' => 'token不存在', 'data' => ''], 405);
        }

        $arr = explode(' ', $authorization);
        $token = $arr[1];
        //jwt进行校验token
        $res = checkToken($token);
        if ($res['status'] != 200) {
            return json(['code' => 999, 'msg' => $res['info'], 'data' => ''], 400);
        }
        $request->uid = $res['data']['uid'];
        return $next($request);
    }
}
